package mysql

import "gorm.io/gorm"

type Goods struct {
	gorm.Model
	GoodsTitle      string  `gorm:"type:varchar(20);not null;comment:商品标题"`
	GoodsCover      string  `gorm:"type:varchar(200);not null;comment:商品封面"`
	GoodsPrice      float64 `gorm:"type:decimal(10,2);not null;comment:商品单价"`
	GoodsStock      int     `gorm:"type:int(10);not null;comment:商品库存"`
	GoodsCollection int     `gorm:"type:int(10);not null;comment:商品收藏量"`
	GoodsState      int     `gorm:"type:int(10);not null;comment:商品状态"`
}

func (g *Goods) GoodsList() (list []*Goods, err error) {
	err = DB.Order("goods_collection desc").Find(&list).Error
	return
}
func (g *Goods) GoodsListOne(goodsId int) (list []*Goods, err error) {
	err = DB.Where("id=?", goodsId).Limit(1).Find(&list).Error
	return
}
func (g *Goods) Where(goodId int) error {
	return DB.Debug().Where("id=?", goodId).Limit(1).Find(&g).Error
}
func (g *Goods) Create() error {
	return DB.Debug().Create(&g).Error
}
func (g *Goods) UpdateGoods(goodsID, GoodsState int) error {
	return DB.Model(&Goods{}).Where("id=?", goodsID).Update("goods_state", GoodsState).Error
}
func (g *Goods) GoodsListState(GoodsState int) (list []*Goods, err error) {
	err = DB.Where("goods_state=?", GoodsState).Find(&list).Error
	return
}
